package com.andrew.servlet;

import java.io.IOException;
import java.math.BigDecimal;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;

import javax.naming.ldap.Rdn;
import javax.security.auth.message.callback.PrivateKeyCallback.Request;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.andrew.entity.CartDetailBean;
import com.andrew.entity.Customer;
import com.andrew.entity.Order;
import com.andrew.entity.OrderDetail;
import com.andrew.service.CartService;
import com.andrew.service.OrderService;
import com.andrew.service.impl.CartServiceImpl;
import com.andrew.service.impl.OrderServiceImpl;


public class InsertOrderByCartServlet extends HttpServlet{
	private OrderService orderService=new OrderServiceImpl();
	private CartService cartService=new CartServiceImpl();
	private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		req.setCharacterEncoding("UTF-8");
		resp.setContentType("text/html;charset=UTF-8");
		
		//生成vc
		Random random=new Random();
		String vc=String.valueOf(random.nextInt(999999-100000+1)+100000);
		
		//得到cartDetail
		List<CartDetailBean> cart=(List<CartDetailBean>)req.getSession().getAttribute("cartDetailBeanList");
		//得到当前用户
		Customer customer=(Customer)req.getSession().getAttribute("customer");
		
		//创建order对象
		Order order=new Order();
		//设置order属性
		order.setOrderId(String.valueOf(System.currentTimeMillis()+customer.getCustomerId()));
		order.setAddress("not set");
		order.setCustomer(customer);
		order.setCreateDate(sdf.format(new Date()));
		order.setSTATUS(-1);
		order.setVC(vc);
		
		//获取订单总价
		BigDecimal totalPrice=new BigDecimal(0);
		//订单条目
		List<OrderDetail> orderDetailsList=new ArrayList<OrderDetail>();
		//遍历购物车信息
		for(CartDetailBean cartDetailBean:cart) {
			//获取总价
			totalPrice=totalPrice.add(cartDetailBean.getPrice());
			//创建订单详情
			OrderDetail od=new OrderDetail();
			od.setBook(cartDetailBean.getBook());//设置订单条目的书本
			od.setQuantity(cartDetailBean.getQuantity());//设置订单条目的数量
			od.setSubTotal(cartDetailBean.getPrice());//设置订单条目的总价
			od.setOrderDetailId(0);//设置订单条目的id
			od.setOrder(order);//设置订单条目所属的订单
			od.setSTATUS(0);
			//添加至订单条目集合中
			orderDetailsList.add(od);
		}
		order.setTotalPrice(totalPrice);
		
		
		//设置当前订单下所有条目
		order.setOrderDetailsList(orderDetailsList);
		
		//清空购物车
		try {
			cartService.clearCart(customer);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		//添加订单
		orderService.addOrder(order);
		//保存至request域，并转发地址
		req.setAttribute("order", order);
		req.getRequestDispatcher("order_payment.jsp").forward(req, resp);
	}
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		this.doGet(req, resp);
	}
}
